//
//  YLStatusViewModel.swift
//  weiBo
//
//  Created by 杨雷 on 2016/12/6.
//  Copyright © 2016年 Yang.L. All rights reserved.
//

import UIKit

class YLStatusViewModel: NSObject {
    
    var status : YLStatus? {
        didSet {
            
            guard let currentStatus = status else {
                return
            }
            //处理会员等级图片
            handleMbrankImage(mbrank: currentStatus.user?.mbrank ?? 0)
            
            //处理会员认证等级
            handleVerifiedTypeImage(verifiedType: currentStatus.user?.verified_type ?? 0)
            
            //处理转发评论点赞的数字
            retweetCountContent = handleCountContent(count: currentStatus.reposts_count, defaultTitle: "转发")
            commentCountContent = handleCountContent(count: currentStatus.comments_count, defaultTitle: "评论")
            unlikeCountContent = handleCountContent(count: currentStatus.attitudes_count, defaultTitle: "赞")
            
        }
        
        
    }
    
    //会员等级
    var mbrankImage : UIImage?
    
    //会员认证等级
    var verifiedTypeImage : UIImage?
    
    //转发数
    var retweetCountContent : String?
    //评论数
    var commentCountContent : String?
    //赞数
    var unlikeCountContent : String?
    
    //转发微博模型
    var retweeted_status : YLStatus?
    
    
    
    //处理toolbar内容
    private func handleCountContent(count : Int , defaultTitle : String) -> String {
        
        if count > 0 {
            if count >= 10000 {
                let result = CGFloat(count / 1000) / 10
                let str = "\(result)万"
                if str.contains(".0"){
                    return str.replacingOccurrences(of: ".0", with: "")
                    
                } else {
                    return str
                }
            } else {
                return "\(count)"
            }
        } else {
            return defaultTitle
        }
        
    }
    
    
    
    
    
    //处理会员认证等级
    private func handleVerifiedTypeImage (verifiedType : Int) {
        switch verifiedType {
        case 1:
            verifiedTypeImage = #imageLiteral(resourceName: "avatar_vip")
        case 2, 3, 5:
            verifiedTypeImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
        case 220:
            verifiedTypeImage = #imageLiteral(resourceName: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
        
        
    }
    
    
    
    
    //处理会员等级图片
    private func handleMbrankImage (mbrank : Int) {
        if mbrank >= 1 && mbrank <= 6 {
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
            
            
        }
        
        
        
    }
    
    
    
    
}
